<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户资金明细表
class Task_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'task';
        $this->subtasks_log_table = 'subtasks_log';
        $this->reload_log_table = 'reload_log';
    }

    /**
     * 体检一个任务
     * @param $insert_arr
     * @return mixed
     */
    function add_task($insert_arr){
        //var_dump($insert_arr);exit();
       return $this->db->insert($this->table,$insert_arr);
    }
    
    function update_task($update_data,$where){
        return $this->db->where($where)->update($this->table, $update_data);
    }
    /**
     * r任务列表
     * @param $page
     * @param $perpage
     * @return mixed
     */
    function task_list($where="",$page,$perpage){
        $page = $page<=0?1:$page;
        $where = $where?$where:1;
        $offset = ($page-1) * $perpage;
        $length = $perpage;
        $sql = "select t.*,u.username,(select count(*) from tan_subtasks st where st.tid=t.tid and st.is_end!=2) as snum from tan_task t 
                left join tan_users u on t.uid=u.user_id where {$where} order by t.is_end asc,t.add_time desc limit $offset,$length";
        $res = $this->db->query($sql)->result_array();
        return $res;
    }
    function task_count($where=""){
        $where = $where?$where:"1=1";
        $sql = "select count(*) as num from tan_task t where {$where}";
        $res = $this->db->query($sql)->row_array();
        return $res['num'];
    }
    /**
     * 任务详情
     * @param $tid
     */
    function task_detail($tid){
        $sql = "select t.*,u.username,(select count(*) from tan_subtasks st where st.tid=t.tid and st.is_end!=2) as snum from tan_task t 
                left join tan_users u on t.uid=u.user_id where tid={$tid}";
        $res = $this->db->query($sql)->row_array();
        return $res;
    }

    function task_remove($tid){
        return $this->db->delete($this->table,array('tid'=>$tid));
    }

    /**
     * 任务是否属于发布方
     * @param $tid
     * @param $uid
     * @return int
     */
    function task_is_my($tid,$uid){
        $sql = "select * from tan_task where tid={$tid} and uid={$uid}";
        $res = $this->db->query($sql)->row_array();
        if(empty($res)){
            return 0;
        }else{
            return 1;
        }
    }
    //获取配置信息
    function website_config(){
        $sql = "select * from tan_website_config limit 1";
        $res = $this->db->query($sql)->row_array();
        return $res;
    }
    //分类列表
    function cate_list(){
        $sql = 'select * from tan_task_cate';
        $res = $this->db->query($sql)->result_array();
        return $res;
    }
    //添加log
    function add_subtasks_log($insert_arr){
        return $this->db->insert($this->subtasks_log_table,$insert_arr);
    }
    //
    function log_content(){
        return array(
            'add_task'=>'任务添加',
            'update_task'=>'任务编辑',
            'add_subtasks'=>'领取任务',
            'validate'=>'提交验证',
            'pay_task'=>'支付任务',
            'close_task'=>'关闭任务',
            'judge_task'=>'验证任务',
            'appeal_pass'=>'申诉通过',
            'appeal_fail'=>'申诉拒绝',
            'cancel_appeal'=>'取消申诉',
            'shenhe_task'=>'审核任务',
            'task_success'=>'任务审核成功',
            'task_fail'=>'任务审核失败',
            'apply_service'=>'申请客服介入',
        );
    }
    function search_list(){
        return array(
            'cate_id'=>array(
                'name'=>'任务类型',
                'data'=>array(
                    '0'=>array(
                        'word'=>'不限',
                    ),
                    '1'=>array(
                        'word'=>'理财任务',
                    ),
                    '2'=>array(
                        'word'=>'注册任务',
                    ),
                    '3'=>array(
                        'word'=>'区间理财',
                    )
                ),
            ),

            'investment_day'=>array(
                'name'=>'理财期限',
                'data'=>array(
                    '0'=>array(
                        'word'=>'不限',
                    ),
                    '1'=>array(
                        'word'=>'0-30天',
                        'min'=>0,
                        'max'=>30
                    ),
                    '2'=>array(
                        'word'=>'30-90天',
                        'min'=>30,
                        'max'=>90
                    ),
                    '3'=>array(
                        'word'=>'90-180天',
                        'min'=>90,
                        'max'=>180
                    ),
                    '4'=>array(
                        'word'=>'180-365天',
                        'min'=>180,
                        'max'=>365
                    ),
                    '5'=>array(
                        'word'=>'365天以上',
                        'min'=>365,
                    ),
                ),
            ),
            'investment_money'=>array(
                'name'=>'起购金额',
                'data'=>array(
                    '0'=>array(
                        'word'=>'不限',
                    ),
                    '1'=>array(
                        'word'=>'0-1000元',
                        'min'=>0,
                        'max'=>1000
                    ),
                    '2'=>array(
                        'word'=>'1000-1万元',
                        'min'=>1000,
                        'max'=>10000
                    ),
                    '3'=>array(
                        'word'=>'1万-5天',
                        'min'=>10000,
                        'max'=>50000
                    ),
                    '4'=>array(
                        'word'=>'5万以上',
                        'min'=>50000,
                    ),
                ),
            ),
            'task_tag'=>array(
                'name'=>'标签',
                'data'=>array(
                    '0'=>array(
                        'word'=>'不限'
                    ),
                    '1'=>array(
                        'word'=>'A股上市',
                    ),
                    '2'=>array(
                        'word'=>'资金存管',
                    ),
                    '3'=>array(
                        'word'=>'银行存管',
                    ),
                    '4'=>array(
                        'word'=>'国资背景',
                    ),
                )
            )
        );
    }

    function add_reload_log($insert_arr){
        return $this->db->insert($this->reload_log_table,$insert_arr);
    }
    function subtask_invest_money_count($tid){
        $sql = "select sum(investment_money) as  investment_money from tan_subtasks where tid={$tid} and is_end!=2";
        $task_info = $this->db->query($sql)->row_array();
        return $task_info['investment_money'];
    }
}
